<?php 
session_start();
ini_set('max_execution_TIME', 600);
include("dbconfig.php");

$db = mysql_pconnect($dbhost, $dbuser, $dbpassword) or die("Connection Error: " . mysql_error());
mysql_query("SET names UTF8");
mysql_select_db($database) or die("Error conecting to db.");



$type=$_GET["type"];
$from_mask=$_GET["from_mask"];
if($type=="") $type=0;
if($type==0)
{
$from_mask=str_replace("-","",$from_mask);
$tb="scanner_".$from_mask;
$res=array();

$query = 'SELECT distinct(ip) as ip from ip_mal';
$out = mysql_query($query) or die('Query failed: ' . mysql_error());
$i=0;
$zero=strtotime("2013-09-30");
while($row = mysql_fetch_array($out,MYSQL_ASSOC)){
	$ip=$row['ip'];
	$q = "SELECT MIN(time) as mi,MAX(time) as mx FROM ip_mal WHERE ip = '".$ip."'";
	$t = mysql_query($q) or die('Query2 failed: ' . mysql_error());
	$trow = mysql_fetch_array($t,MYSQL_ASSOC);
	$st=strtotime($trow['mi']);
	$ed=strtotime($trow['mx']);
	$dur=($ed-$st)/86400;
	//$o = $intervalo->format("Years:%Y,Months:%M,Days:%d,Hours:%H,Minutes:%i,Seconds:%s");
	$tozero=($st-$zero)/86400;
	$res[$ip]=array($tozero,$tozero+$dur);
	$i=$i+1;
	//if($i>10) break;
}

echo json_encode($res);
// Free resultset

}
if($type==1)
{
$from_mask=str_replace("-","",$from_mask);
$tb="scanner_".$from_mask;
$res=array();
$query = 'SELECT a.port as port,b.reg as reg, count(*) as count FROM '.$tb.' a, ip_reg  b WHERE a.ip = b.ip group by b.reg,a.port';
$res1 = mysql_query($query) or die('Query failed: ' . mysql_error());

while($row = mysql_fetch_array($res1) ){

	array_push($res,array($row['port'],$row['reg'],intval($row['count'])));
}
echo json_encode($res);

}
?>
